﻿namespace Foxby.Core.MetaObjects
{
	using DocumentFormat.OpenXml;
	using DocumentFormat.OpenXml.Wordprocessing;

	/// <summary>
	/// Class which holds basic information about <see cref="SdtElement"/>
	/// </summary>
	public class Field
	{
		private readonly SdtElement _element;

		internal Field(SdtElement element)
		{
			_element = element;
		}

		/// <summary>
		/// Name of underlying <see cref="SdtElement"/>
		/// </summary>
		public string Name
		{
			get { return SdtAlias.Val.Value; }
			set { SdtAlias.Val = new StringValue(value); }
		}

		/// <summary>
		/// Name of tag of underlying <see cref="SdtElement"/>
		/// </summary>
		public string Tag
		{
			get { return SdtAlias.Val.Value; }
			set { SdtAlias.Val = new StringValue(value); }
		}

		private SdtAlias SdtAlias
		{
			get
			{
				SdtProperties properties = _element.SdtProperties;

				return properties.GetFirstChild<SdtAlias>() ??
				       properties.AppendChild(new SdtAlias {Val = new StringValue()});
			}
		}
	}
}